package com.leetcode.June;

import javax.swing.tree.TreeNode;

/**
 * @Description: 二叉树的直径
 * @Author: Mr.Fxy
 * @CreateTime: 2024-07-25 09:33
 */
public class BinaryTree {

    int ans = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        searchDepth(root);
        return ans;
    }

    public int searchDepth(TreeNode node) {
        if (node == null) return 0;
        int left = searchDepth(node.left);
        int right = searchDepth(node.right);
        ans = Math.max(left+right, ans);
        return Math.max(left, right)+1;
    }
}
